package com.hlxt.nj.sys.service.impl;

import com.hlxt.nj.common.PageBean;
import com.hlxt.nj.sys.dao.TUserDAO;
import com.hlxt.nj.sys.model.TUser;
import com.hlxt.nj.sys.model.TUserCriteria;
import com.hlxt.nj.sys.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class TUserServiceImpl implements TUserService {

    @Autowired
    private TUserDAO userDao;


    @Override
    public void queryPageList(TUser condition, PageBean pageBean) {
        userDao.selectPageBean(condition,pageBean);
    }

    @Override
    public TUser findByLoginname(String loginName) {
        TUserCriteria Tcriteria = new TUserCriteria();
        TUserCriteria.Criteria criteria = Tcriteria.createCriteria();
        criteria.andLoginnameEqualTo(loginName);
        List<TUser> list=userDao.selectByExample(Tcriteria);
        if(list!=null && list.size()!=0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public TUser findByPnumber(String pNumber,String usename) {
        TUserCriteria Tcriteria = new TUserCriteria();
        TUserCriteria.Criteria criteria = Tcriteria.createCriteria();
        criteria.andPnumberEqualTo(pNumber);
        criteria.andUsernameEqualTo(usename);
        List<TUser> list=userDao.selectByExample(Tcriteria);
        if(list!=null && list.size()!=0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public boolean add(TUser user) {
        try{
            userDao.insertSelective(user);
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public TUser getByUserid(String userid) {
        return userDao.selectByPrimaryKey(userid);
    }

    @Override
    public boolean update(TUser user) {
        try{
            userDao.updateByPrimaryKeySelective(user);
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteUsers(String ids) {
        try{
            userDao.deleteByPrimaryKey(ids);
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


}
